package models

import (
	"errors"
	"github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/orm"
	"time"
	"tools"
)

type User struct {
	Id            string `orm:"column(id);pk;size(64)" json:"id" form:"id"`
	Mobile        int64  `orm:"column(mobile)" json:"mobile" form:"mobile"`
	Password      string `orm:"column(password)" json:"-" form:"password"`
	CreateTime    int64  `orm:"column(create_time)" json:"create_time" form:"create_time"`
	LastLoginTime int    `orm:"column(last_login_time)" json:"last_login_time" form:"last_login_time"`
	LastLoginIp   int    `orm:"column(last_login_ip)" json:"last_login_ip" form:"last_login_ip"`
	Status        int    `orm:"column(status)" json:"status" form:"status"`
	NickName      string `orm:"column(nick_name)" json:"nick_name" form:"nick_name"`
	Birthday      string `orm:"column(birthday)" json:"birthday" form:"birthday"`
	Sex           int    `orm:"column(sex)" json:"sex" form:"sex"`
	Avatar        string `orm:"column(avatar)" json:"avatar" form:"avatar"`
}

func (m *User) TableName() string {
	return "user"
}

func init() {
	orm.RegisterModel(new(User))
}

func NewUser() *User {
	return &User{}
}

func GetUserById(id string) (*User, error) {
	user := &User{Id: id}
	err := orm.NewOrm().Read(user)
	if err == orm.ErrNoRows {
		return nil, nil
	}
	if err != nil {
		logs.Warn("GetUserById error:", err)
		return nil, err
	}
	return user, nil
}

func GetUserByMobile(mobile int64) (*User, error) {
	user := &User{Mobile: mobile}
	err := orm.NewOrm().Read(user, "mobile")
	if err == orm.ErrNoRows {
		return nil, nil
	}
	if err != nil {
		logs.Warn("GetUserByMobile error:", err)
		return nil, err
	}
	return user, nil
}

func GetUserByName(name string) (*User, error) {
	user := &User{NickName: name}
	err := orm.NewOrm().Read(user, "nick_name")
	if err == orm.ErrNoRows {
		return nil, nil
	}
	if err != nil {
		logs.Warn("GetUserByName error:", err)
		return nil, err
	}
	return user, nil
}

// @title 用户是否存在
func IsExistsUser(mobile int64) bool {
	user, err := GetUserByMobile(mobile)
	if err != nil || user == nil {
		return false
	}
	return true
}

// @title register user
func AddUser(mobile int64, password string) error {
	user := &User{
		Id:         tools.GUUID(),
		Mobile:     mobile,
		Password:   tools.MD5Encode(password),
		Sex:        2,
		CreateTime: time.Now().Unix(),
	}
	_, err := orm.NewOrm().Insert(user)
	if err != nil {
		logs.Warn("AddUser error:", err)
		return err
	}
	return nil
}

// @title login
func Login(mobile int64, password string) (*User, error) {
	user, err := GetUserByMobile(mobile)
	if err != nil {
		logs.Warn("user login error:", err)
		return nil, err
	}
	if user == nil {
		err = errors.New("用户不存在")
		logs.Warn("user login error:", err)
		return nil, err
	}
	if user.Password != tools.MD5Encode(password) {
		err = errors.New("密码不正确")
		logs.Warn("user login error:", err)
		return nil, err
	}
	return user, nil
}

// 修改用户信息
func UpdateUserById(id, nick_name, birthday, avatar string, sex int) error {
	sql := `UPDATE user SET nick_name = ?, birthday = ?, avatar = ?, sex = ? WHERE id = ?`
	_, err := orm.NewOrm().Raw(sql, nick_name, birthday, avatar, sex, id).Exec()
	if err != nil {
		logs.Warn("user update error:", err)
		return err
	}
	return nil
}

// 修改密码
func ResetUserPassword(mobile int64, password string) error {
	sql := "UPDATE `user` SET `password` = ? WHERE `mobile` = ?"
	rs, err := orm.NewOrm().Raw(sql, tools.MD5Encode(password), mobile).Exec()
	if err != nil {
		logs.Warn("user register error:", err)
		return err
	}
	if num, err := rs.RowsAffected(); err != nil || num == 0 {
		logs.Warn("user register error: 修改失败")
		return orm.ErrNoRows
	}
	return nil
}
